﻿using GodSoft.Common;
using GodSoft.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace GodSoft.Controllers
{
    public class LightController : BaseController
    {
        GodSoftContext _context = new GodSoftContext();

        public ActionResult GetLightTypeComboboxList()
        {
            var list = from i in _context.DeviceTypes
                       orderby i.ID
                       select new
                       {
                           LightTypeID = i.ID,
                           LightTypeName = i.Type_Name
                       };

            return Json(list, JsonRequestBehavior.AllowGet);
        }
       public ActionResult Index()
        {
            return View();
        }
       
        [HttpGet]  
        //默认加载数据  
        public ActionResult SearchLight()  
        {  
           var LightList = from i in _context.Lights
                            orderby i.ID
                            select new
                            {
                             i.LightName,//路灯名
                             i.CenterLng,//经度
                             i.CenterLat,//纬度
                             i.Zoom,//比例尺
                             i.Address,//地址
                             i.CreateTime,//创建日期
                             i.Status,//路灯状态
                             i.Brand,//路灯品牌
                             i.Company,//路灯维护公司
                             i.ID//路灯编号
                            };

            StringBuilder str = new StringBuilder();  
            foreach (var Light in LightList)  
            {  
                str.Append(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9};",
                             Light.LightName,//路灯名
                             Light.CenterLng,//经度
                             Light.CenterLat,//纬度
                             Light.Zoom,//比例尺
                             Light.Address,//地址
                             Light.CreateTime,//创建日期
                             Light.Status,//路灯状态
                             Light.Brand,//路灯品牌
                             Light.Company,//路灯维护公司
                             Light.ID//路灯维护公司
 
                      ));  
            }
          return Json(str.ToString(), JsonRequestBehavior.AllowGet);  
        }

        [HttpPost]
        public ActionResult Regist(Light Light)
        {
                _context.Lights.Add(Light);
                _context.SaveChanges();
                return Content("OK");
        }
      
        public ActionResult LightInfoShow()
        {
            return View();
        }

        public ActionResult LightStateSearch(int? StateID)
        {
            if (StateID != null)
            {
                ViewBag.StateID = StateID;
            }
            return View();
        }

        public ActionResult GetAll(int? StateID)
        {
            int pageIndex = Request["page"] == null ? 1 : int.Parse(Request["page"]);
            int pageSize = Request["rows"] == null ? 10 : int.Parse(Request["rows"]);

            if (StateID == null)
            {
                var temp = from i in _context.Lights
                           from j in _context.DeviceTypes
                           where i.TypeID==j.ID
                           orderby i.ID
                           select new
                           {
                               i.ID,
                               i.TypeID,//路灯类型编号
                               j.Type_Name,//路灯类型名
                               i.LightName,
                               i.CenterLng,
                               i.CenterLat,
                               i.Zoom,
                               i.Address,
                               i.CreateTime,
                               i.Status,
                               i.Brand,
                               i.Company,
                               i.Information,
                               i.Province,
                               i.City,
                               i.District,
                               i.Street,
                               i.StreetNumber,
                               i.Logo
                           };
                var list = temp.Skip((pageIndex - 1) * pageSize).Take(pageSize);
                var result = new { total = temp.Count(), rows = list };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var temp = from i in _context.Lights
                           from j in _context.DeviceTypes
                           where i.TypeID == j.ID&&i.Status == StateID
                           orderby i.ID
                           select new
                           {
                               i.ID,
                               i.TypeID,//路灯类型编号
                               j.Type_Name,//路灯类型名
                               i.LightName,
                               i.CenterLng,
                               i.CenterLat,
                               i.Zoom,
                               i.Address,
                               i.CreateTime,
                               i.Status,
                               i.Brand,
                               i.Company,
                               i.Information,
                               i.Province,
                               i.City,
                               i.District,
                               i.Street,
                               i.StreetNumber,
                               i.Logo
                           };
                var list = temp.Skip((pageIndex - 1) * pageSize).Take(pageSize);
                var result = new { total = temp.Count(), rows = list };
                return Json(result, JsonRequestBehavior.AllowGet);
            
            
            }
         
            }


        public ActionResult UpdateInfo(Light light)
        {
            _context.Lights.Attach(light);
            _context.Entry<Light>(light).State = EntityState.Modified;
            _context.SaveChanges();
            return Content("OK");
        }

        public ActionResult DeleteLight(int? ID)
        {
            if (ID != null)
            {
                if (_context.Lights.Any(i => i.ID == ID))
                {
                    _context.Lights.Remove(_context.Lights.Find(ID));
                    _context.SaveChanges();
                    return Content("OK");

                }
                else
                {
                    return Content("不存在该条记录,请您检查!");
                }
            }
            else
            {
                return Content("删除失败,请您检查!");
            }

        }
        public ActionResult DeleteInfo(string deleteInfoID)
        {
            if (string.IsNullOrEmpty(deleteInfoID))
            {
                return Content("请选择您要删除的数据");
            }
            //截取传递过来的字符串的数字信息
            var idsStr = deleteInfoID.Split(',');

            List<int> deleteIDList = new List<int>();

            foreach (var ID in idsStr)
            {
                deleteIDList.Add(Convert.ToInt32(ID));
            }
            if (DeleteManyInfoes(deleteIDList) > 0)
            {
                return Content("OK");
            }
            else
            {
                return Content("删除失败，请您检查");
            }

        }

        public int DeleteManyInfoes(List<int> DeleteIDList)
        {
            Light Light = new Light();
            foreach (var id in DeleteIDList)
            {
                Light = _context.Lights.Where(f => f.ID == id).FirstOrDefault();
                _context.Lights.Remove(Light);
            }
            return _context.SaveChanges();
        }
        public ActionResult EditLightState()
        {
            string EditLightIDs = Request["EditStateIDs"];
            int EditStatus = Convert.ToInt32(Request["EditStatus"]);
            if (string.IsNullOrEmpty(EditLightIDs))
            {
                return Content("请选择您要修改的数据");
            }
            //截取传递过来的字符串的数字信息
            var idsStr = EditLightIDs.Split(',');

            List<int> editIDList = new List<int>();

            foreach (var ID in idsStr)
            {
                editIDList.Add(Convert.ToInt32(ID));
            }

            if (EditManyLightsState(editIDList, EditStatus) > 0)
            {
                return Content("OK");
            }
            else
            {
                return Content("修改失败，请您检查");
            }

        }
        public int EditManyLightsState(List<int> EditIDList,int EditStatus)
        {
            foreach (var id in EditIDList)
            {
                Light Light = _context.Lights.Where(f => f.ID == id).FirstOrDefault();
                Light.Status = EditStatus;
                _context.Lights.Attach(Light);
                _context.Entry<Light>(Light).State = EntityState.Modified;
                
             }
            return _context.SaveChanges();
        }
        //修改路灯(地图标注)状态
        public ActionResult EditLightLabelState()
        {
            string EditLightID = Request["EditStateID"];
            int ID;
            int EditStatus = Convert.ToInt32(Request["EditStatus"]);
            if (string.IsNullOrEmpty(EditLightID))
            {
                return Content("请选择您要修改的数据!");
            }
            else
            {
                ID=Convert.ToInt32(Request["EditStateID"]);
                if (_context.Lights.Any(f => f.ID == ID))
                {
                    Light Light = _context.Lights.Find(ID);
                    Light.Status = EditStatus;
                    _context.Lights.Attach(Light);
                    _context.Entry<Light>(Light).State = EntityState.Modified;
                    _context.SaveChanges();
                    return Content("OK");
                }
                else
                {
                    return Content("修改失败,请您检查!");
                }
              
            }
           
        }

        public ActionResult BindLightInfo(int ID)
        {
            var jsonData = from i in _context.Lights
                            where i.ID==ID
                            orderby i.ID
                            select new
                            {
                                i.ID,
                                i.LightName,
                                i.CenterLng,
                                i.CenterLat,
                                i.Zoom,
                                i.Address,
                                i.CreateTime,
                                i.Status,
                                i.Brand,
                                i.Company,
                                i.Information,
                                i.Province,
                                i.City,
                                i.District,
                                i.Street,
                                i.StreetNumber,
                                i.Logo
                            };
            
            return Json(jsonData.First(), JsonRequestBehavior.AllowGet);
        }
    }
}
